//
//  ARPictureTableViewController.m
//  AriseSamePro
//
//  Created by 包日守 on 15/10/31.
//  Copyright © 2015年 Arise. All rights reserved.
//

#import "ARPictureTableViewController.h"
#import "ARPictureCell.h"
#import "ARPictureController.h"

@interface ARPictureTableViewController ()<UIViewControllerPreviewingDelegate>

@end

@implementation ARPictureTableViewController

- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location NS_AVAILABLE_IOS(9_0)
{
    
    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:location];
    
    
    ARPictureCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    

    
    CGPoint convertedLocation = [self.tableView convertPoint:location toView:cell.theMainImageView];
    if (CGRectContainsPoint(cell.theMainImageView.bounds, convertedLocation)) {
        
        ARPictureController *pVC = [[ARPictureController alloc] init];
        
        if(cell.theMainImageView.image){
        
        pVC.arrayOfImages = @[cell.theMainImageView.image];
        pVC.index = 0;
        return pVC;
        }

    }
    
    
    return nil;
}
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit NS_AVAILABLE_IOS(9_0)
{
    [self presentViewController:viewControllerToCommit animated:NO completion:nil];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        [self registerForPreviewingWithDelegate:(id)self sourceView:self.view];
        NSLog(@"3D Touch is available!");
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat hight = 472 - 350 - 18;
    ARCate5Model *cateModel = self.cateModels[indexPath.section];
    
    if (cateModel.photo) {
        hight += 380;
    }
    
    if (cateModel.txt != nil) {
        
        CGRect contentRect = [cateModel.txt boundingRectWithSize:CGSizeMake(320, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
        
        
        hight += contentRect.size.height;
        
    }
    
    return hight;
    
    

}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ARPictureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ARPictureCell"];
    
    if (cell == nil) {
        
        
        [tableView registerNib:[UINib nibWithNibName:@"ARPictureCell" bundle:nil] forCellReuseIdentifier:@"ARPictureCell"];
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"ARPictureCell"];
        
    }
    cell.cateModel = self.cateModels[indexPath.section];
    
    
    [cell setShareBtnBlock:^(ARCate5Model *cateModel, UIButton *sender) {
        [self createShare:cateModel WithSender:sender];
        
    }];
    
    cell.theMainImageView.superViewController = self;
    
    return cell;


}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
